import 'package:flutter/material.dart';
import 'package:flutter_massage/common_lib/extension/ex_widget.dart';
import 'package:flutter_massage/common_lib/index.dart';
import 'package:flutter_massage/common_lib/routers/route.dart';
import 'package:flutter_massage/common_lib/ui/common_component.dart';
import 'package:flutter_massage/generated/assets.dart';
import 'package:flutter_massage/pages/home/home_component.dart';
import 'package:flutter_massage/pages/home/home_logic.dart';
import 'package:flutter_massage/pages/preject/project_page.dart';
import 'package:flutter_massage/pages/technician/technician_list/technician_list_page.dart';
import 'package:flutter_massage/service/index.dart';
import 'package:flutter_massage/values/index.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

/// @Author 杨璐铭
/// @Date 2023/8/3 10:47 PM
/// @describe 首页
class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin {
  final String _tag = DateTime.now().toString();

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return _HomeComponent(_tag);
  }

  @override
  bool get wantKeepAlive => true;
}

class _HomeComponent extends GetView<HomeLogic> {
  const _HomeComponent(this.tag1);

  final String tag1;

  @override
  String? get tag => tag1;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<HomeLogic>(
      tag: tag1,
      assignId: true,
      init: HomeLogic(),
      builder: (logic) {
        return BaseScaffold(
          hideBackBtn: true,
          scaffoldBackgroundColor: AppColors.grayF8,
          child: Stack(
            children: [
              _buildBg(),
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  // _buildLocation().paddingOnly(top: ScreenUtil().statusBarHeight + 10.w, bottom: 14.w),
                  const SizedBox().paddingOnly(top: ScreenUtil().statusBarHeight),
                  SingleChildScrollView(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        _buildLocation().paddingTop(14.w),
                        _buildRecommendJs().paddingOnly(top: 14.w),
                        _buildAd().paddingOnly(top: 16.w),
                        _buildRecommendProject().paddingOnly(top: 16.w),
                      ],
                    ),
                  ).expanded(),
                ],
              ),
            ],
          ),
        );
      },
    );
  }

  Widget _buildBg() {
    return AspectRatio(
      aspectRatio: 1.17,
      child: IconWidget.image(
        Assets.imageHomeTopBg,
        width: ScreenUtil().screenWidth,
        fit: BoxFit.cover,
      ),
    );
  }

  Widget _buildLocation() {
    return ValueListenableBuilder(
      valueListenable: controller.address,
      builder: (BuildContext context, String value, Widget? child) {
        return ButtonWidget.iconText(
          Assets.svgHomeLocation.toSvg(width: 14.w),
          value.getAuto('开启定位'),
          fontSize: 13,
          onTap: () {
            LocationService.instance.openLocationSetting();
          },
        ).paddingLeft(12.w);
      },
    );
  }

  Widget _buildRecommendJs() {
    return OneModuleComponent(
        '附近推荐技师',
        () {
          return LayoutBuilder(
            builder: (BuildContext context, BoxConstraints constraints) {
              double spacing = 6.w;
              double width = ((constraints.maxWidth - spacing * 2) / 3).floorToDouble();
              return Wrap(
                spacing: 6.w,
                children: controller.technicianList.map((e) {
                  return HomeRecommendTechnician(e).width(width);
                }).toList(),
              ).paddingTop(6.w);
            },
          );
        },
        titleFontWeight: FontWeight.bold,
        titleFontSize: 15,
        all: '更多',
        onTap: () async {
          var result = await JsonService.getTechnicianList();
          CommonRoute.open(TechnicianListPage(result));
        }).paddingHorizontal(12.w);
  }

  Widget _buildRecommendProjectTitle() {
    return Container(
      decoration: CommonDecoration.getRadius(
        topLeft: 8.w,
        topRight: 8.w,
        gradient: LinearGradient(
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
          stops: const [0, 1],
          colors: ['#FEEAEA'.toColor, '#FCF2F4'.toColor],
        ),
      ),
      padding: EdgeInsets.only(left: 8.w, right: 12.w, top: 9.w, bottom: 17.w),
      child: Row(
        children: [
          ImageWidget.asset(Assets.imageIconFire, width: 21.w),
          const TextWidget.oneLine(
            text: '附近推荐项目',
            fontSize: 16,
            color: AppColors.textLevelOne,
            fontWeight: FontWeight.bold,
          ).paddingLeft(4.w),
          const Spacer(),
          ButtonWidget.textIcon(
            '更多',
            Assets.svgArrowRightGray.toSvg(width: 9.w),
            fontSize: 13,
            fontWeight: FontWeight.bold,
            spacing: 4.w,
            onTap: () => CommonRoute.open(ProjectPage()),
            // onTap: () => CommonRoute.open(TechnicianAlbumPage()),
          ),
        ],
      ),
    );
  }

  Widget _buildRecommendProject() {
    return Stack(
      children: [
        _buildRecommendProjectTitle(),
        Column(
          children: controller.projectList.map((e) => HomeRecommendProject(e).paddingBottom(8.w)).toList(),
        ).paddingTop(38.w),
      ],
    ).paddingHorizontal(12.w);
  }

  Widget _buildAd() {
    return Obx(() {
      return CarouselWidget(
        items: controller.bannerList.map((e) => e.image ?? '').toList(),
        boxFit: BoxFit.cover,
        currentIndex: controller.currentIndex.value,
        onPageChanged: (i, _) => controller.onPageChanged(i),
      );
    }).paddingSymmetric(horizontal: 12.w);
  }
}
